home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / accounts / acct-1.3 / acct-1 / acct-1.3.73 / utils / acctentries.c < prev    next >
C/C++ Source or Header  |  1996-03-12  |  2KB  |  96 lines

  1. /*
  2.  * acctentries.c 
  3.  *
  4.  * A small program to print out what is stored in /var/adm/acct file
  5.  *
  6.  * Author: Juha Virtanen<jiivee@iki.fi>
  7.  *
  8.  * For a copyright apply GPL
  9.  *
  10.  * Last time modified on 12th MArch 1996
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <linux/acct.h>
  22.  
  23. #define PUT_TO_SBIN
  24. #define DATE_STRING_LEN 64
  25.  
  26. int main (int argc, char *argv[])
  27. {
  28.     struct acct ac[sizeof (struct acct)];
  29.     long file_len=0;
  30.     long entries=0;
  31.     char starttime[DATE_STRING_LEN];
  32.     char endtime[DATE_STRING_LEN];
  33.     char *tmptime;
  34.     time_t etime;
  35.     FILE *fp;
  36.     char filename[FILENAME_MAX+1];
  37.  
  38.     if (argc == 2)
  39.     {
  40.         strcpy (filename, argv[1]);
  41.         if ((fp = fopen (filename, "rb")) == NULL)
  42.         {
  43.             fprintf (stderr, "Could not open %s\n", filename);
  44.             exit (3);
  45.         }
  46. #ifndef PUT_TO_SBIN
  47.         fprintf (stderr, "Opened %s for reading\n", filename);
  48. #endif
  49.     }
  50.     else
  51.     {
  52.         fprintf (stderr, "Usage: %s filename\n", argv[0]);
  53.         exit (1);
  54.     }
  55.     if ((fread ((void *) ac, sizeof (struct acct), 1, fp)) != 1)
  56.     {
  57.         fprintf (stderr,
  58.              "Too short file or read error in the beginning.\n");
  59.         exit (5);
  60.     }
  61.     etime=ac->ac_btime + ac->ac_etime;
  62.     tmptime = ctime(&etime);
  63.     strcpy (starttime, tmptime);
  64.     if ((fseek (fp, 0, SEEK_END)) != 0)
  65.     {
  66.         fprintf (stderr, "Fseek error\n");
  67.         exit (9);
  68.     }
  69.     if ((file_len = ftell (fp)) < 0)
  70.     {
  71.         fprintf (stderr, "Ftell error\n");
  72.         exit (17);
  73.     }
  74.     if ((fseek (fp, (- (sizeof (struct acct))), SEEK_CUR)) != 0)
  75.     {
  76.         fprintf (stderr, "Fseek error\n");
  77.         exit (10);
  78.     }
  79.     if ((fread ((void *) ac, sizeof (struct acct), 1, fp)) != 1)
  80.     {
  81.         fprintf (stderr, "Read error.\n");
  82.         exit (6);
  83.     }
  84.     etime=ac->ac_btime + ac->ac_etime;
  85.     tmptime = ctime(&etime);
  86.     strcpy (endtime, tmptime);
  87.     entries = file_len / (sizeof (struct acct));
  88. #ifdef PUT_TO_SBIN
  89.     printf ("%ld\n%s%s", entries, starttime, endtime);
  90. #else
  91.     printf ("%s has %ld entries\nFirst entry: %s last entry: %s",
  92.         filename, entries, starttime, endtime);
  93. #endif
  94.     return (0);
  95. }
  96.